package com.clouddot.baipai.infrastructure.environment.initial;

import android.text.TextUtils;

import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.infrastructure.environment.appContext;
import com.clouddot.baipai.model.beam.TargetUrl;
import com.clouddot.baipai.model.beam.getVersionInfo.XMLBean;
import com.clouddot.baipai.ui.util.Ftp.AnalyzeXMLUtil;
import com.clouddot.baipai.ui.util.ToastUtil;
import com.clouddot.baipai.ui.util.netConnectUtil;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;

/**
 * Created by wxy on 2018/4/9.
 */

public class InitialAppContext extends InitialBase {
    private Environment Environment;

    public InitialAppContext(Environment Environment) {
        super(Environment);
        this.Environment = Environment;
    }

    @Override
    public String getName() {
        return super.getName();
    }
    /**
     * 设置参数
     * --- 班牌设备号
     * --- 密码
     * --- ip地址
     * --- 自动更新功能
     * --- 自动更新时间
     * --- 自动拍照
     * --- 刷脸功能
     * --- 刷脸访问的url
     * --- 刷脸识别度
     */
    @Override
    public void initial() throws Exception {
        super.initial();
        Environment.appContext = new appContext();

        Environment.appContext.carddevid = netConnectUtil.getMacAddress(Env.getContent());
        Environment.appContext.password = (String) Environment.helper.get("password","123");
        Environment.appContext.scanVerify = (int) Environment.helper.get("scanVerify",80);
        Environment.appContext.itemCount = Environment.Height <= 100 ? 11 : 15;
        File file = new File(Env.getAppContext().CONFIGPATH);
        if(file.exists()) {
            FileInputStream in = new FileInputStream(file);
            XMLBean xmlBean = AnalyzeXMLUtil.parseXML(in);
            if (!TextUtils.isEmpty(xmlBean.getUrl())) {
                Environment.appContext.host = xmlBean.getUrl();
                Environment.appContext.appName = !TextUtils.isEmpty(xmlBean.getData()) ? xmlBean.getData() : Environment.appContext.appName;
                Environment.appContext.IP = "http://" + Environment.appContext.host;
                Environment.appContext.content = "/msjsAjaxHandlerRoad/MobileAction.msjs?host=" + URLEncoder.encode(Environment.appContext.host);

                Environment.appContext.isAutoUpdate = (boolean) Environment.helper.get("isAuto", true);
                Environment.appContext.autoUpdateTime = (int) Env.getHelper().get("time", 2);
                Environment.appContext.isAutoTakePhoto = (boolean) Environment.helper.get("isAutoTakePhoto", false);
                Environment.appContext.isOpenService = (boolean) Environment.helper.get("isOpenService", true);
                Environment.appContext.openScan = (boolean) Environment.helper.get("openScan", false);
                Environment.appContext.scanFaceUrl = (String) Environment.helper.get("scanFaceUrl", "");
                Environment.appContext.backMainTime = (int) Environment.helper.get("backMainTime",5);
                Environment.appContext.isInitial = true;
            } else {

                Environment.appContext.isInitial = false;
            }

            Environment.targetUrl = new TargetUrl();

        }else{
            ToastUtil.showToast(Environment.context,"配置文件缺失，请双击顶部进入设置界面进行配置");
        }

    }
    @Override
    public void reinitial() throws Exception {
        super.reinitial();

        Environment.appContext = new appContext();
        Environment.appContext.carddevid = netConnectUtil.getMacAddress(Env.getContent());
        Environment.appContext.password = (String) Environment.helper.get("password","123");
        Environment.appContext.scanVerify = (int) Environment.helper.get("scanVerify",80);
        Environment.appContext.itemCount = Environment.Height <= 100 ? 11 : 15;
        File file = new File(Env.getAppContext().CONFIGPATH);
        if(file.exists()) {
            FileInputStream in = new FileInputStream(file);
            XMLBean xmlBean = AnalyzeXMLUtil.parseXML(in);
            Environment.appContext.host = xmlBean.getUrl();
            Environment.appContext.appName = !TextUtils.isEmpty(xmlBean.getData()) ? xmlBean.getData() : Environment.appContext.appName;
            Environment.appContext.IP = "http://" + Environment.appContext.host;
            Environment.appContext.content = "/msjsAjaxHandlerRoad/MobileAction.msjs?host=" + URLEncoder.encode(Environment.appContext.host);
            Environment.appContext.isAutoUpdate = (boolean) Environment.helper.get("isAuto", true);
            Environment.appContext.autoUpdateTime = (int) Env.getHelper().get("time", 02);
            Environment.appContext.isAutoTakePhoto = (boolean) Environment.helper.get("isAutoTakePhoto", false);
            Environment.appContext.isOpenService = (boolean) Environment.helper.get("isOpenService", true);
            Environment.appContext.openScan = (boolean) Environment.helper.get("openScan", false);
            Environment.appContext.scanFaceUrl = (String) Environment.helper.get("scanFaceUrl", "");
            Environment.appContext.backMainTime = (int) Environment.helper.get("backMainTime",5);
            Environment.appContext.isInitial = true;

            Environment.targetUrl = new TargetUrl();

        }else{
            ToastUtil.showToast(Environment.context,"配置文件缺失，请双击顶部进入设置界面进行配置");
        }
    }
}
